/*jslint browser: true, cap: false, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function(){

    //Module-level members:
    var client = SONGHAY.ui.Client;
    var dom = SONGHAY.ui.Dom;

    SONGHAY.ui.Client.dataForm =
    {
        alert: function(type, args)
        {
            if (args.length) { } else { return; }

            if (args.length > 1)
            {
                client.alertFocus = args[1];
                dom.alert(client.applicationTitle, args[0], self.alertProcedure);
            }
            else
            {
                dom.alert(client.applicationTitle, args[0], self.alertProcedure);
            }
        },

        alertFocus: null,

        alertProcedure: function()
        {
            if (this.hide) { this.hide(); }

            if (client.alertFocus && client.alertFocus.focus)
            {
                client.alertFocus.focus();
            }
        },

        confirm: function(e)
        {
            YAHOO.util.Event.stopEvent(e);

            var o = YAHOO.util.Event.getTarget(e);
            var oForm = null;

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;

            if(o && o.id && o.form && oEventPassThrough && oEventPassThrough.serverCommand)
            {
                oForm = o.form;

                oEventPassThrough.target = o;

                self.confirm.srcEventPassThrough = oEventPassThrough;

                switch(o.id)
                {
                    case 'DeleteDocument':
                        dom.confirm(client.applicationTitle,
                            'Are you sure you want to delete Document ' + oForm.DocumentId.value + '?',
                            self.confirmProcedure);
                    break;

                    case 'DeleteFragment':
                        dom.confirm(client.applicationTitle,
                            'Are you sure you want to delete Fragment ' + oForm.FragmentId.value + '?',
                            self.confirmProcedure);
                    break;

                    case 'DeleteSegment':
                        dom.confirm(client.applicationTitle,
                            'Are you sure you want to delete Segment ' + oForm.SegmentId.value + '?',
                            self.confirmProcedure);
                    break;
                }
            }
        },

        confirmProcedure: function()
        {
            if(this.hide) { this.hide(); }

            var oEventPassThrough = self.confirm.srcEventPassThrough;

            client.ajax.postAndDisplay(null,oEventPassThrough);
        },

        formatTextArea: function(vTextAreaId) {
            var o = null;

            //Format textarea elements:
            o = YAHOO.util.Dom.get(vTextAreaId);
            if (o) {
                if (o.innerHTML == '&#160;') { o.innerHTML = ''; }
                if (o.innerHTML == '&nbsp;') { o.innerHTML = ''; }

                try {
                    o.wrap = 'soft';
                }
                catch (e) { }
            }
        },

        inputIsCheck: function(e)
        {
            var oElement = YAHOO.util.Event.getTarget(e);
            if(oElement && (oElement.value !== undefined)) { oElement.value = (oElement.checked) ? 1 : 0; }
        },

        loadAppSettings: function()
        {
            var vSuccess = false;

            client.settings = YAHOO.util.Dom.get('ApplicationProperties');
            if (client.settings)
            {
                vSuccess = true;

                if (!client.settings.DataConnectionId)
                {
                    alert('Not all of the application settings were found.');
                    vSuccess = false;
                }
            }

            return vSuccess;
        }
    };

    //Module-level "this":
    var self = client.dataForm;

    client.ui.DataValidation.onMessage.subscribe(self.alert);

})();
